package controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import po.LoginMessage;
import service.InsertServiceImpl;
import service.JudgeServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class TestController {
    //日志文件类
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private JudgeServiceImpl judgeServiceImpl;
    @Autowired
    private InsertServiceImpl insertServiceImpl;


    //测试
    @ResponseBody
    @RequestMapping("/validationTest")
    public Map login(@RequestBody LoginMessage loginMessage, HttpSession session) {

        boolean judgeResult = false;
        judgeResult = judgeServiceImpl.loginValidation(loginMessage);
        Map<String,Object> resultMap = new HashMap();

        if (judgeResult == true){
            session.setAttribute("loginUser", loginMessage);

            resultMap.put("url","homeTest");
            resultMap.put("result",judgeResult);

            return resultMap;
        }

        resultMap.put("url","homeTest");
        resultMap.put("result",judgeResult);

        return resultMap;

    }

    //测试@RequestParam注解
    @ResponseBody
    @RequestMapping("/test1")
    public Map login2(@RequestBody Map<String,Integer> map) {

        boolean judgeResult = false;
        Map<String,Object> resultMap = new HashMap();


        resultMap.put("url","homeTest");
        resultMap.put("result",judgeResult);

        return resultMap;

    }


    @RequestMapping("/loginTest")
    public String  loginTest() throws Exception{
        return "loginTest";
    }

    @RequestMapping("/homeTest")
    public String  homeTest() throws Exception{
        return "homeTest";
    }

    @RequestMapping("/logoutTest")
    public String  logoutTest(HttpSession session) throws Exception{
        session.removeAttribute("loginUser");
        session.invalidate();
//        return "loginTest";
        return "redirect:/loginTest";
    }

    @RequestMapping("/test")
    public String  test() throws Exception{
        return "test";
    }

    @ResponseBody
    @RequestMapping("/noCacheTest")
    public Map  test(HttpSession session) throws Exception{
        Map<String,Object> resultMap = new HashMap();
        if (session.getAttribute("loginUser") != null){
            resultMap.put("judgeResult",true);
        }else {
            resultMap.put("judgeResult",false);
        }

        return resultMap;
    }
}
